#!/bin/sh
NVM_NODE_VER=`cat .nvmrc`

rm -f .initver

PROFILE_NAME=".bash_profile"
PROFILE_PATH="${HOME}/${PROFILE_NAME}"
touch $PROFILE_PATH

rm -f .nvmrc
echo "sourcing..."
. ${PROFILE_PATH}


# install node version
HAS_NODE=`nvm ls ${NVM_NODE_VER} | awk '{print $1}'`
if [ "${HAS_NODE}" = "N/A" ]
then
echo "installing node ${NVM_NODE_VER}..."
nvm install ${NVM_NODE_VER}
echo "done."
else
echo "node ${NVM_NODE_VER} already installed."
fi



#! use the project node version
echo ${NVM_NODE_VER} > .nvmrc
nvm use


# do work
if [ ! -f ".lock" ]
then
touch .lock

# install global packages
npm install \
    --silent \
    --registry=https://registry.npm.taobao.org \
    -g shrinkpack

fi
# clean
echo "cleaning, please wait..."
rm -rf node_modules/
echo "upgrading packages, please wait..."
npm install --production --silent --registry=https://registry.npm.taobao.org
echo "done."

